package com.booklove.service.voluntary;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booklove.constant.DeliverStateConstant;
import com.booklove.constant.DeliverTypeConstant;
import com.booklove.constant.VoluntaryWorkType;
import com.booklove.constant.VoluntaryworkStateConstant;
import com.booklove.context.BaseContext;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.Books;
import com.booklove.domain.po.Deliver;
import com.booklove.domain.po.Donatebook;
import com.booklove.domain.po.Voluntarywork;
import com.booklove.domain.query.VoluntaryworkQuery;
import com.booklove.domain.vo.VoluntaryworkVO;
import com.booklove.mapper.BooksMapper;
import com.booklove.mapper.VoluntaryworkMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booklove.service.delivery.DeliverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 志愿任务 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-22
 */
@Service
public class VoluntaryworkServiceImpl extends ServiceImpl<VoluntaryworkMapper, Voluntarywork> implements VoluntaryworkService {

    @Autowired
    BooksMapper booksMapper;
    @Autowired
    VoluntaryworkMapper voluntaryworkMapper;

    @Autowired
    DeliverService deliverService;

    @Override
    public PageDTO<VoluntaryworkVO> queryVoluntaryworksPage(VoluntaryworkQuery voluntaryworkQuery) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则
        List<OrderItem> orderItems = voluntaryworkQuery.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<Voluntarywork> page = voluntaryworkQuery.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
        /*
        查询
         */
        Page<VoluntaryworkVO> p = baseMapper.selectPageVO(page, voluntaryworkQuery.getName());

        // 获取数据信息
        return PageDTO.of(p, VoluntaryworkVO.class);
    }

    //TODO 修复志愿任务
    @Override
    public void createCollectDonateBookWord(Donatebook donatebook) {
        Books book = booksMapper.selectById(donatebook.getBookId());
        String title = "收书任务" + book.getName();
        // 自动填充title，content，cover
        Voluntarywork voluntarywork = createVoluntaryWork(title, donatebook.getRecommend(), book.getCover(), VoluntaryWorkType.GET_BOOK);

        // 设置关联的捐书id
        voluntarywork.setDonateBookId(donatebook.getId());

        // 创建派送任务
        Deliver deliver = deliverService.createDeliver(DeliverTypeConstant.TAKE,
                DeliverStateConstant.WAITING_RECEIVE_ORDER,
                null,
                donatebook.getAddressId());
        // 回填deliver的id
        voluntarywork.setDeliverId(deliver.getId());
    }

    /**
     * @description 创建一个志愿工作-不设置类型
     * @author zken
     * @CreateDate 2024/10/19 18:21:54
     */
    public Voluntarywork createVoluntaryWork(String title, String content, String picture, Integer type) {
        Voluntarywork voluntarywork = new Voluntarywork();
        // 设置任务类型
        voluntarywork.setVoluntaryWorkType(type);
        voluntarywork.setTitle(title);
        voluntarywork.setContent(content);
        voluntarywork.setPicture(picture);
        // 设置任务状态
        voluntarywork.setVoluntaryWorkState(VoluntaryworkStateConstant.WAITING_ACCEPT);

        // 基本填充
        Integer userId = BaseContext.getCurrentId().intValue();
        voluntarywork.setCreatedBy(userId);
        voluntarywork.setCreatedTime(LocalDateTime.now());
        voluntarywork.setUpdatedBy(userId);
        voluntarywork.setUpdatedTime(LocalDateTime.now());

        return voluntarywork;
    }

}
